<template>
  <div class="sidebar-menu">
    <button @click="toggleMenu">收起/展开菜单</button>
    <transition name="slide">
      <ul v-show="showMenu">
        <li :class="{active: activeIndex=== index}" v-for="(item, index) in menuItems" :key="item.id" @click="handleItemClick(item, index)">
          {{ item.title }}
        </li>
      </ul>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'CollapsibleSidebarMenu',
  props: {
    menuItems: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      showMenu: true,
      activeIndex: 0
    };
  },
  methods: {
    toggleMenu() {
      this.showMenu = !this.showMenu;
    },
    handleItemClick(item,index) {
        this.activeIndex = index
      console.log('菜单项被点击:', item);
    }
  }
};
</script>

<style scoped>
.sidebar-menu {
  background-color: #f0f0f0;
  padding: 10px;
}

button {
  margin-bottom: 10px;
}

.slide-enter-active,
.slide-leave-active {
  transition: transform 0.5s;
}

.slide-enter,
.slide-leave-to {
  transform: translateX(-100%);
}
.active {
    background-color: red;
}
</style>